﻿using System;
using System.Net;
using static CatUtils.Utils.Currency.PrintLog;

namespace CatUtilsDll.Utils.Currency.WebUtils
{
    public partial class WebHelper
    {
        /// <summary>
        /// Pc请求UserAgent
        /// </summary>
        public static string UserAgentPC { get; set; } = PcUserAgentFireFoxSetVersion(73);

        /// <summary>
        /// 手机请求UserAgnet
        /// </summary>
        public static string UserAgentMobile { get; set; } = " Mozilla/5.0 (Linux; Android 8.0; SM-G892A Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/67.0.3396.87 Mobile Safari/537.36";

        /// <summary>
        /// 电脑请求头
        /// </summary>
        /// <param name="RandomVersion">设置随机版本信息</param>
        /// <returns></returns>
        public static WebHeaderCollection PcWebHeader(bool RandomVersion = false)
        {
            string TempUserAgentPc = UserAgentPC;
            if (RandomVersion)
            {
                TempUserAgentPc = PcUserAgentFireFoxRandomVersion();
            }

            WebHeaderCollection webHeaderCollection = new WebHeaderCollection
            {
            { "User-Agent",TempUserAgentPc},

                { "Accept-Language", "zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3" }
        };
            return webHeaderCollection;
        }

        /// <summary>
        /// 手机请求头，android7.0
        /// </summary>
        /// <returns></returns>
        public static WebHeaderCollection MobileWebHeader()
        {
            WebHeaderCollection webHeaderCollection = new WebHeaderCollection
            {
                { "User-Agent",UserAgentMobile },

                { "Accept-Language", "zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3" }
            };
            return webHeaderCollection;
        }

        /// <summary>
        /// 通用获取webheader类
        /// </summary>
        /// <param name="FileUrl"></param>
        /// <returns></returns>
        public static WebHeaderCollection PcWebHeader(string FileUrl)
        {
            WebHeaderCollection webHeaderCollection = PcWebHeader();

            try
            {
                webHeaderCollection.Add("Host", new Uri(FileUrl).Host);
            }
            catch (Exception ex)
            {
                Log($"WebHelper.PcWebHeader，初始化链接:{FileUrl}Host失败。");
                Log(ex);
            }

            return webHeaderCollection;
        }

        /// <summary>
        /// 通用获取webheader类
        /// </summary>
        /// <param name="FileUrl"></param>
        /// <returns></returns>
        public static WebHeaderCollection MobileWebHeader(string FileUrl)
        {
            WebHeaderCollection webHeaderCollection = MobileWebHeader();

            try
            {
                webHeaderCollection.Add("Host", new Uri(FileUrl).Host);
            }
            catch (Exception ex)
            {
                Log($"WebHelper.MobileWebHeader，初始化链接:{FileUrl}Host失败。");
                Log(ex);
            }

            return webHeaderCollection;
        }

        #region 获取火狐PcUserAgent

        /// <summary>
        /// 获取WindowsHeader
        /// </summary>
        public string PcUserAgentFireFox = PcUserAgentFireFoxSetVersion(72);

        /// <summary>
        /// 获取随机的Windows Header
        /// get random Windows header
        /// </summary>
        /// <returns></returns>
        public static string PcUserAgentFireFoxRandomVersion()
        {
            return PcUserAgentFireFoxSetVersion(new Random().Next(50, 85));
        }

        /// <summary>
        /// 设置指定版本的WindowsHeader
        /// WindowsHeader SetVersion
        /// </summary>
        /// <param name="Version"></param>

        /// <returns></returns>
        public static string PcUserAgentFireFoxSetVersion(double Version)
        {
            string VirsionText = Version.ToString("0.0");//需要格式化具体版本
            return $"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:{VirsionText}) Gecko/20100101 Firefox/{VirsionText}";
        }

        #endregion 获取火狐PcUserAgent

        #region 获取Google PC Header

        /// <summary>
        /// Google 浏览器PC header
        /// Google Pc Header
        /// </summary>
        public static string PcUserAgentGoogle = PcUserAgentGoogleSetVersion(79);

        /// <summary>
        /// 获取随机的Windows Header
        /// get random Windows header
        /// </summary>
        /// <returns></returns>
        public static string PcUserAgentGoogleRandomVersion()
        {
            return PcUserAgentGoogleSetVersion(new Random().Next(50, 85));
        }

        /// <summary>
        ///获取WindowsHeader，设置版本和内核版本
        ///Get windows header ，set Vsersion
        /// </summary>
        /// <param name="Version">浏览器版本  Brower Version</param>
        /// <returns></returns>
        public static string PcUserAgentGoogleSetVersion(double Version)
        {
            return PcUserAgentGoogleSetVersion(Version, new Random().Next(500, 650) + new Random().NextDouble());
        }

        /// <summary>
        /// 获取WindowsHeader，设置版本和内核版本
        /// Get windows header ，set Vsersion and  AppleWebKit VserSion
        /// </summary>
        /// <param name="Version">浏览器版本</param>
        /// <param name="AppleWebKitVserSion">使用内核版本</param>
        /// <returns></returns>
        public static string PcUserAgentGoogleSetVersion(double Version, double AppleWebKitVserSion)
        {
            return $"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/{AppleWebKitVserSion.ToString("0.00")} (KHTML, like Gecko) Chrome/{Version.ToString("0.0")}.{new Random().Next(100, 5000)}.{new Random().Next(100, 5000)} Safari /537.36";
        }

        #endregion 获取Google PC Header
    }
}